<template>
  <div class="container">
    <el-card :style="{maxHeight: contentHeight * 0.8 + 'px'}" class="box-card">
      <div class="title">卡初始化</div>
      <el-form ref="dataFormRef" :model="dataForm" label-width="120px" style="padding: 30px;" @submit.native.prevent>
          <el-form-item>
            <el-button icon="el-icon-s-open" round type="primary" @click="initCard">初始化</el-button>
          </el-form-item>
        <el-form-item v-show="false">
          <el-button icon="el-icon-s-open" round type="danger" @click="resetCard">重置卡</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="home-btn">
      <div class="btn-item">
        <el-button icon="el-icon-back" round size="small" type="primary" @click="goBackHome"> 返 回</el-button>
      </div>
      <div class="btn-item">
        <el-button icon="el-icon-search" round size="small" type="info" @click="goCardInfo">卡查询</el-button>
      </div>
      <div class="btn-item">
        <el-button icon="el-icon-s-finance" round size="small" type="primary" @click="goMakeMaster">开主卡</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import {isNewEmptyCard, initCard, isValidCard, resetCard} from '@/utils/cardreader'
export default {
  name: 'CardInit',
  data () {
    return {
      inited: true,
      dataForm: {
        idCardNo: '',
        phone: ''
      },
      dataList: []
    }
  },
  computed: {
    ...mapGetters(['contentHeight', 'operator', 'stationId', 'stationName', 'shiftName'])
  },
  methods: {
    goBackHome () {
      this.$router.push('/card')
    },
    goCardInfo () {
      this.$router.push('/card/info')
    },
    goMakeMaster () {
      this.$router.push('/card/master')
    },
    initCard () {
      console.log('initCard')
      var err = isNewEmptyCard()
      if (err) {
        this.$message.warning('该卡不是有效的空白卡')
        return false
      }
      err = initCard()
      if (err) {
        this.$message.error('卡初始化失败')
        return false
      }
      this.$message.success('卡初始化成功！')
    },
    resetCard () {
      console.log('resetCard')
      var err = isValidCard()
      if (err) {
        this.$message.warning('该卡不是有效卡')
        return false
      }
      err = resetCard()
      if (err) {
        this.$message.error('重置卡失败' + err)
        return false
      }
      this.$message.success('重置卡成功！')
    }
  }
}
</script>

<style scoped>
.el-select {
  width: 100%;
}

.box-card {
  width: 60%;
  margin-left: 20%;
  position: absolute;
  top: 4%;
  overflow-y: auto;
}

.home-btn {
  position: absolute;
  top: 2%;
  left: 2%;
}

.btn-item {
  margin-bottom: 20px;
}

.title {
  text-align: center;
  padding-top: 1%;
  font-size: 1.6rem;
  color: #666;
}

.card-form {
  background: white;
  padding: 20px;
}
</style>